package com.nema.util;

import com.nema.infer.NemaInfer;
import lsh.LshPackage.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import software.lib.service.LshCaller;
import software.lib.service.NemaService;

import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class QueryByTitle {
	int MIN = 0;
	int MAX = Integer.MAX_VALUE;
	
	List<String> score = new ArrayList<String>();
	List<String> timespan = new ArrayList<String>();
	List<String> duration = new ArrayList<String>();
	List<String> price_low = new ArrayList<String>();
	List<String> price_high = new ArrayList<String>();
	List<String> tags = new ArrayList<String>();
	
	List<String> sale_effiency = new ArrayList<String>();
	List<String> country = new ArrayList<String>();
	List<String> language = new ArrayList<String>();
	List<String> level = new ArrayList<String>();
	List<String> avg_resp_time = new ArrayList<String>();
	


	ResultSet ret = null;
	String test = "";
	
	public Map<String, Object> query(String title){
		//new TrainData().init();
		Map<String, Object> result = new HashMap<>();
		Pair[] pairs = LshCaller.getInstance().candidate(title);
		result.put("pairs",pairs);

		int sim_cnt = Math.min(LshCaller.MAX_RANGE,pairs.length);
		result.put("sim_cnt", sim_cnt+"");

		List<Integer> GID = Arrays.stream(pairs).limit(sim_cnt).map(pair -> pair.id).collect(Collectors.toList());
		try (Connection conn = dataSource.getConnection()){
			for(int gid:GID){
				PreparedStatement preparedStatement =
						conn.prepareStatement("select score,timespan,duration,price_low,price_high,tags,seller_id " +
								"sale_effiency,country,language,level,avg_resp_time" +
								" from gig,`user` where id=seller_id and gig_id='"+gid+"'");
				ret = preparedStatement.executeQuery();
				if(ret.next()){
					score.add(ret.getString(1));
					timespan.add(ret.getString(2));
					duration.add(ret.getString(3));
					price_low.add(ret.getString(4));
					price_high.add(ret.getString(5));
					tags.add(ret.getString(6));
					//sql = "select sale_effiency,country,language,level,avg_resp_time from user where id='"+ret.getString(7)+"'";
					//ret = db.conn.prepareStatement(sql).executeQuery();
					//if(ret.next()){
					sale_effiency.add(ret.getString(8));
					country.add(ret.getString(9));
					language.add(ret.getString(10));
					avg_resp_time.add(ret.getString(12));
					//}
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		int max = MIN;
		int min = MAX;
		for(String str:score){
			if(Double.parseDouble(str) > max)
				max = Integer.parseInt(str);
			if(Double.parseDouble(str) < min)
				min = Integer.parseInt(str);
		}
		result.put("score", min+"-"+max);
		
		max = MIN;
		min = MAX;
		for(String str:timespan){
			if(Double.parseDouble(str) > max)
				max = Integer.parseInt(str);
			if(Double.parseDouble(str) < min)
				min = Integer.parseInt(str);
		}
		result.put("timespan", min+"-"+max);
		
		max = MIN;
		min = MAX;
		for(String str:duration){
			if(Double.parseDouble(str) > max)
				max = Integer.parseInt(str);
			if(Double.parseDouble(str) < min)
				min = Integer.parseInt(str);
		}
		result.put("duration", min+"-"+max);
		
		max = MIN;
		min = MAX;
		for(String str:price_low){
			if(Double.parseDouble(str) < min)
				min = Integer.parseInt(str);
		}
		for(String str:price_high){
			if(Double.parseDouble(str) > max)
				max = Integer.parseInt(str);
		}
		result.put("price", min+"-"+max);
		
		max = MIN;
		min = MAX;
		for(String str:avg_resp_time){
			if(Double.parseDouble(str) > max)
				max = Integer.parseInt(str);
			if(Double.parseDouble(str) < min)
				min = Integer.parseInt(str);
		}
		result.put("avg_resp_time", min+"-"+max);
		
		double max_ = MIN;
		double min_ = MAX;
		for(String str:sale_effiency){
			if(Double.parseDouble(str) > max_)
				max_ = Double.parseDouble(str);
			if(Double.parseDouble(str) < min_)
				min_ = Double.parseDouble(str);
		}
		result.put("sale_effiency", min_+"-"+max_);

		HashSet<String> hs=new HashSet<String>();
		for(String str:country){
			hs.add(str);
		}
		String temp = "";
		for (String str : hs) {  
			temp += str+";";
		}  
		result.put("country", temp.substring(0, temp.length()-1));
		
		hs=new HashSet<String>();
		for(String str:language){
			String[] arr = str.split("\\|");
			for(String s : arr){
				if(s.length() > 3)
					hs.add(s);
			}		
		}
		temp = "";
		for (String str : hs) {  
			temp += str+";";
		}  
		result.put("language", temp.substring(0, temp.length()-1));
		
		hs=new HashSet<String>();
		for(String str:tags){
			String[] arr = str.split("\\|");
			for(String s : arr){
				hs.add(s);
			}		
		}
		temp = "";
		for (String str : hs) {  
			temp += ";"+str;
		}  
		result.put("tag", temp.substring(1));
		
		result.put("gig", title);
		return result;
	}

	@Autowired
	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
}
